<template>
  <div >
    <div style="height: 200px"></div>
    <h1 style="font-size: 60px;color: black;margin: 0 auto;text-align: center">欢迎光临网上考试报名系统</h1>
    <div style="background: #D3D3D3; width: 500px; margin: 0px auto;border-radius: 20px; padding: 20px 50px;">
      <h1 style="text-align: center;margin: 30px auto" >登录页面</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="name">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>

          <el-button @click="gotoReg()">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style>
body{
  /* background-image:url('/public/images/login2.jpg');*/
  /*background-repeat: no-repeat;*/
  /*background-size: cover;*/
  /*z-index: 1;*/
  background-color: #ffffff;
}
</style>
<script>
import jwtDecode from "jwt-decode";

export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password:'123456'
      },
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://120.77.154.166:8080/user/login' ;
          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData)
          this.axios.post(url,formData).then((response => {
            let jsonResult = response.data;
            console.log(response)
            if (jsonResult.state === 20000) {
              this.$message({
                message: '恭喜你，登录成功',
                type: 'success'
              });
              // 将服务器端响应的JWT存储到localStorage
              let jwt = jsonResult.data;
              console.log('jwt = ' + jwt);
              let decode = jwtDecode(jwt);
              let username = decode.username;
              console.log('username = ' + username);
              localStorage.setItem('username',username)
              localStorage.setItem('jwt', jwt);
              console.log('已经将JWT存储到localStorage');
              this.$router.push('/sys-index/index') //  跳转到主页
            }else if (response.data.state === 40100) {
              this.$message.error(response.data.message);
            }else {
              this.$message(response.data.message);
            }
          }));
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    gotoReg(){
      this.$router.push('/reg') // 重定向到注册页面
    },


  }
}
</script>
